package com.wing.sys.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.wing.common.CommonSpringJDBC;
import com.wing.sys.domain.Auth;

@Repository
public class AuthDao extends CommonSpringJDBC<Auth, String> {

	private Auth generateAuth(Map map){
		Auth r =new Auth();
		r.setId((String)map.get("id"));
		r.setOwner_id((String)map.get("owner_id"));
		r.setRes_id((String)map.get("res_id"));
		r.setType((String)map.get("type"));
		return r;
	}
	
	public List<Auth> getAuthsByTypeResId(String type, String resId) {
		String sql =" select * from sys_auth where type = '"+type+"' and res_id='"+resId+"'";
		List list = this.getSimpleJdbcTemplate().queryForList(sql);
		List list1 = new ArrayList();
		for(int i=0;i<list.size();i++){
			Map map = (Map)list.get(i);
			list1.add(generateAuth(map));
		}
		return list1;
	}

	public List getAuthsByTypeOwnerId(String type, String ownerId) {
		String sql =" select * from sys_auth where type = '"+type+"' and owner_id='"+ownerId+"'";
		List list = this.getSimpleJdbcTemplate().queryForList(sql);
		List list1 = new ArrayList();
		for(int i=0;i<list.size();i++){
			Map map = (Map)list.get(i);
			list1.add(generateAuth(map));
		}
		return list1;
	}

	public List getAuthsByTypeOwnerIdResId(String type, String ownerId,String resId) {
		String sql =" select * from sys_auth where type = '"+type+"' and owner_id='"+ownerId+"' and res_id='"+resId+"' ";
		List list = this.getSimpleJdbcTemplate().queryForList(sql);
		List list1 = new ArrayList();
		for(int i=0;i<list.size();i++){
			Map map = (Map)list.get(i);
			list1.add(generateAuth(map));
		}
		return list1;
	}

	public void deleteAuthsByTypeOwnerId(String type, String ownerId) {
		String sql = "delete from sys_auth where type=? and owner_id=?";
		this.getSimpleJdbcTemplate().update(sql, type,ownerId);
	}

	public void deleteAuthsByTypeResId(String type, String resId) {
		String sql = "delete from sys_auth where type=? and res_id=?";
		this.getSimpleJdbcTemplate().update(sql, type,resId);
	}

	
}
